package com.ylkj.wx.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ylkj.wx.entitis.EmployeeEntity;
import com.ylkj.wx.service.EmployeeInfoService;
import com.ylkj.wx.utils.zxing.QRCodeParams;
import com.ylkj.wx.utils.zxing.QRCodeUtil;
import com.ylkj.wx.utils.zxing.QRParamsException;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
	@Autowired
	public EmployeeInfoService employeeService;
	
	@RequestMapping(value = "/getEmployeeQRCode", method = RequestMethod.GET)
	public @ResponseBody EmployeeEntity getEmployeeQRCode(HttpServletRequest request) {
		String bizCode = request.getParameter("bizCode");
		String employeeCode = request.getParameter("employeeCode");
		String path = request.getContextPath();  
		//网络目录 
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
		//绝对目录
		String realPath = request.getSession().getServletContext().getRealPath("/");  
		EmployeeEntity employee = employeeService.getEmployeeByCodeAndBizCode(bizCode, employeeCode);
		if(employee==null){
			return null;
		}
		QRCodeParams params=new QRCodeParams();
		String fileName=bizCode+"_"+employeeCode+".png";
		params.setFileName(fileName);

		String filePath = realPath+"res/img/qrcode/";
		params.setFilePath(filePath);
		//http://puyfg37476.jsp.cdnjsp.cn/ylkj/reward/rindex.do?employeeId=1
		params.setTxt(basePath+"reward/rindex.do?phrCode="+employee.getPhrCode());
		if(employee.getHeaderImgUrl()==null||employee.getHeaderImgUrl().equalsIgnoreCase("")){
			//默认头像
			params.setLogoPath(realPath+"res/img/qrcode/defaultLogo.png");
		}else{
			params.setLogoPath(realPath+employee.getHeaderImgUrl());
		}
	    try {
			QRCodeUtil.generateQRImage(params);
		} catch (QRParamsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			employee.setQrCodeUrl("");
			return employee;
		}
	    employee.setQrCodeUrl(basePath+"res/img/qrcode/"+fileName);
	   //employeeService.updateQrCodeUrl(employee.getId(), basePath+"res/img/qrcode/"+fileName);
		return employee;
	}
	
	@RequestMapping(value = "/getEmployeeById", method = RequestMethod.GET)
	public @ResponseBody EmployeeEntity getEmployeeById(HttpServletRequest request) {
		String employeeId = request.getParameter("employeeId");
		String path = request.getContextPath();  
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
		//EmployeeEntity employee = employeeService.getEmployeeById(employeeId);
		EmployeeEntity employee = new EmployeeEntity();
		employee.setId(Long.parseLong(employeeId));
		return employee;
	}

}
